﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Text;

namespace FCNS.KeepMoneyLibrary.Data
{
    public class GtdEntryField
    {
        public void Remove()
        {
            KMData.DataTableGtdEntrys.Rows.Remove(dRow);
        }

        public static List<GtdEntryField> GetGtdEntryField(DateTime? startTime, DateTime? endTime, bool ShowOverdueData)
        {
            List<GtdEntryField> fields = new List<GtdEntryField>();

            StringBuilder sb = new StringBuilder();
            if (startTime != null)
            {
                sb.Append("StartDateTime >= '" + startTime + "'");
                if (endTime != null)
                    sb.Append(" and EndDateTime <= '" + endTime + "'");
            }
            else if (endTime != null)
                sb.Append("EndDateTime <= '" + endTime + "'");

            if (!ShowOverdueData)
            {
                if (sb.Length > 0)
                    sb.Append(" and ");

                sb.Append("Finish = false");
            }

            foreach (DataRow row in KMData.DataTableGtdEntrys.Select(sb.ToString()))
                fields.Add(new GtdEntryField(row));

            return fields;
        }

        bool add = false;
        public void AddRowToDataTable()
        {
            if (!add)
                return;

            KMData.DataTableGtdEntrys.Rows.Add(dRow);
            add = false;
        }

        DataRow dRow = null;
        public GtdEntryField(DataRow row)
        {
            Debug.Assert(row != null);
            dRow = row;
        }

        public GtdEntryField(int GtdGroupId)
        {
            dRow = KMData.DataTableGtdEntrys.NewRow();
            dRow["GtdGroupId"] = GtdGroupId;
            add = true;
        }

        public DateTime StartTime
        {
            get { return (DateTime)dRow["StartDateTime"]; }
            set { dRow["StartDateTime"] = value; }
        }

        public DateTime EndTime
        {
            get { return (DateTime)dRow["EndDateTime"]; }
            set { dRow["EndDateTime"] = value; }
        }

        public bool Finish
        {
            get
            {
                return (bool)dRow["Finish"];
            }
            set
            {
                dRow["Finish"] = value;
            }
        }

        public string Title
        {
            get
            {
                return (string)dRow["Name"];
            }
            set
            {
                dRow["Name"] = value;
            }
        }

        public string Note
        {
            get
            {
                return (string)dRow["Note"];
            }
            set
            {
                dRow["Note"] = value;
            }
        }

        public int Level
        {
            get { return (int)dRow["Level"]; }
            set { dRow["Level"] = value; }
        }

        public GtdGroupField GtdGroup
        {
            get { return new GtdGroupField(KMData.DataTableGtdGroups.Select("Id = " + dRow["GtdGroupId"])[0]); }
            set { dRow["GtdGroupId"] = value.Id; }
        }
    }
}
